﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Configuration;
using System.IO;

using SharpPopulatorLib;

namespace SharpPopulator 
{

    public partial class Database : Form 
    {

        #region Constructor

        public Database() 
        {
            InitializeComponent();
            cboxProviders.DataSource = Settings.GetProviders();
        }

        #endregion

        #region Events

        private void cboxProviders_SelectedIndexChanged(object sender, EventArgs e) 
        {
            //
            Settings.Provider = cboxProviders.SelectedText;
        }

        #endregion

        #region Private Methods

        private static List<String> GetProviders()
        {
            //To obtain providers form the DBProviders file

            List<String> providers = new List<String>();
            XDocument doc = null;
            string localFolder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string fullPath = String.Format(@"{0}\{1}", localFolder, ConfigurationManager.AppSettings["DBProviders"]);

            try
            {
                if (File.Exists(fullPath))
                { doc = XDocument.Load(fullPath); }
                else
                {
                    DirectoryInfo d = new DirectoryInfo(localFolder).Parent;
                    if (d.GetFiles(ConfigurationManager.AppSettings["DBProviders"]).Length == 1)
                    { doc = XDocument.Load(d.GetFiles(ConfigurationManager.AppSettings["DBProviders"])[0].FullName); }
                    else
                    { doc = XDocument.Load(d.Parent.GetFiles(ConfigurationManager.AppSettings["DBProviders"])[0].FullName); }
                }
                if (doc != null)
                {
                    providers = (from xnode in doc.Element("Providers").Elements("Provider")
                                 select xnode.Element("Name").Value).ToList();
                }
            }
            catch
            { }
            return providers;
        }

        private void btnConnect_Click(object sender, EventArgs e) 
        {
            //To connect to the source and retrive tables

            this.PopulateSettings();
            if (string.IsNullOrEmpty(Settings.Server) || string.IsNullOrEmpty(Settings.DBInstance) || string.IsNullOrEmpty(Settings.User)) 
            { MessageBox.Show("Server, DB Instance and User are required"); } 
            else 
            {
                List<String> tables = DBManager.GetTables(Settings.ConnectionString);

                if (tables.Count == 0) 
                { MessageBox.Show("No tables found in target database"); } 
                else 
                {
                    Forms.frmPopulatorV2 populator = new Forms.frmPopulatorV2(tables);
                    populator.MdiParent = this.ParentForm;
                    populator.Show();
                    this.Close();
                }
            }
        }

        /// <summary>
        /// To populate de settings data
        /// </summary>
        private void PopulateSettings()
        {
            //To populate settings
            Settings.Provider = cboxProviders.SelectedItem.ToString();
            Settings.Server = txtServer.Text.ToString();
            Settings.DBInstance = txtDB.Text.ToString();
            Settings.User = txtUser.Text.ToString();
            Settings.UserPassword = txtUserPassword.Text.ToString();
        }

        #endregion

    }

}
